﻿using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Enum;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class OrderCouponDAL : BaseDAL<OrderCouponDAL>
	{
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(OrderCoupon model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into OrderCoupon(");
            strSql.Append("OrderId,ConsumerCouponId,CouponId,ConsumerId,CouponValue,EnableCouponWithDraw,IsDelete");
            strSql.Append(") values (");
            strSql.Append("@OrderId,@ConsumerCouponId,@CouponId,@ConsumerId,@CouponValue,@EnableCouponWithDraw,@IsDelete");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ConsumerCouponId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CouponId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CouponValue", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@EnableCouponWithDraw", SqlDbType.Bit,1) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.OrderId;
            parameters[1].Value = model.ConsumerCouponId;
            parameters[2].Value = model.CouponId;
            parameters[3].Value = model.ConsumerId;
            parameters[4].Value = model.CouponValue;
            parameters[5].Value = model.EnableCouponWithDraw;
            parameters[6].Value = model.IsDelete; var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(OrderCoupon model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update OrderCoupon set ");

            strSql.Append(" OrderId = @OrderId , ");
            strSql.Append(" ConsumerCouponId = @ConsumerCouponId , ");
            strSql.Append(" CouponId = @CouponId , ");
            strSql.Append(" ConsumerId = @ConsumerId , ");
            strSql.Append(" CouponValue = @CouponValue , ");
            strSql.Append(" EnableCouponWithDraw = @EnableCouponWithDraw , ");
            strSql.Append(" IsDelete = @IsDelete  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ConsumerCouponId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CouponId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CouponValue", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@EnableCouponWithDraw", SqlDbType.Bit,1) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.OrderId;
            parameters[2].Value = model.ConsumerCouponId;
            parameters[3].Value = model.CouponId;
            parameters[4].Value = model.ConsumerId;
            parameters[5].Value = model.CouponValue;
            parameters[6].Value = model.EnableCouponWithDraw;
            parameters[7].Value = model.IsDelete; return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("update OrderCoupon set IsDelete=1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("update OrderCoupon set IsDelete=1 ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModel(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Id, OrderId, ConsumerCouponId, CouponId, ConsumerId, CouponValue, EnableCouponWithDraw, IsDelete  ");
            strSql.Append("  from OrderCoupon ");
            strSql.Append(" where Id=@Id  and IsDelete=0");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM OrderCoupon where IsDelete=0 ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion
		
		#region 扩展方法

        /// <summary>
        /// 获取订单使用的优惠券列表
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
	    public List<OrderCoupon> GetListByOrderId(int orderId)
	    {
            var strSql = new StringBuilder();
            strSql.Append("SELECT oc.Id, oc.OrderId, oc.ConsumerCouponId, oc.CouponId, oc.ConsumerId, ");
            strSql.Append("oc.CouponValue, oc.EnableCouponWithDraw, oc.IsDelete,");
            strSql.Append("cp.CouponName, cp.CouponCategoryId, cc.CategoryCode, cc.CategoryName ");
            strSql.Append("FROM OrderCoupon oc ");
            strSql.Append("INNER JOIN Coupon cp ON cp.Id = oc.CouponId ");
            strSql.Append("INNER JOIN CouponCategory cc ON cp.CouponCategoryId=cc.Id ");
            strSql.Append("WHERE oc.IsDelete=0  AND oc.OrderId=@OrderId AND cp.IsDelete=0 AND cc.IsDelete=0 ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4)
			};
            parameters[0].Value = orderId;

	        var lsRet = DbHelperSQL.ExecuteSqlList<OrderCoupon>(strSql.ToString(), parameters);
	        return lsRet == null || lsRet.Count == 0 ? null : lsRet.ToList();
	    }

	    /// <summary>
	    /// 获取指定用户在指定店铺使用指定优惠券ID下单的订单优惠券使用记录
	    /// </summary>
	    /// <param name="consumerId"></param>
	    /// <param name="shopId"></param>
	    /// <param name="commodityLibId"></param>
	    /// <param name="consumerOrderType"></param>
	    /// <param name="couponId"></param>
	    /// <returns></returns>
	    public List<OrderCoupon> GetListOfCouponIdWithConsumerIdBuyingFromShops(int consumerId, int shopId,
            int commodityLibId, int consumerOrderType, int couponId)
        {
            var sbSql = new StringBuilder();
            sbSql.Append("SELECT DISTINCT oc.Id, oc.OrderId, oc.ConsumerCouponId, oc.CouponId, oc.ConsumerId, ");
            sbSql.Append("oc.CouponValue, oc.EnableCouponWithDraw, oc.IsDelete,");
            sbSql.Append("cp.CouponName, cp.CouponCategoryId, cc.CategoryCode, cc.CategoryName ");

            sbSql.Append("FROM OrderCoupon oc ");
            sbSql.Append("INNER JOIN Coupon cp ON cp.Id = oc.CouponId ");
            sbSql.Append("INNER JOIN CouponCategory cc ON cp.CouponCategoryId=cc.Id ");
            sbSql.Append("INNER JOIN Orders os ON os.id=oc.OrderId ");
            sbSql.Append("INNER JOIN OrderDetail od ON os.Id=od.OrderId ");
            sbSql.Append("INNER JOIN ShopCommodity sc ON od.ShopCommodityId=sc.Id ");
            sbSql.Append("INNER JOIN CommodityLib cl ON sc.CommodityLibId=cl.Id ");

            sbSql.Append("WHERE oc.IsDelete=0 AND cp.IsDelete=0 AND cc.IsDelete=0 ");
            sbSql.Append("AND cl.Id=@CommodityLibId AND sc.ShopId=@ShopId ");
            sbSql.Append("AND os.ConsumerId=@ConsumerId AND os.OrderStatus=@OrderStatus ");
            sbSql.Append("AND od.IsDelete=0 AND os.IsDelete=0 AND os.ConsumerOrderType=@ConsumerOrderType ");
            sbSql.Append("AND oc.CouponId=@CouponId ");

            var lsParameters = new List<SqlParameter>
            {
                new SqlParameter("@CommodityLibId", commodityLibId),
                new SqlParameter("@ShopId", shopId),
                new SqlParameter("@ConsumerId", consumerId),
                new SqlParameter("@OrderStatus", (int) CommonEnum.OrderStatus.Finished),
                new SqlParameter("@ConsumerOrderType", consumerOrderType),
                new SqlParameter("@CouponId", couponId)
            };

            var lsRet = DbHelperSQL.ExecuteSqlList<OrderCoupon>(sbSql.ToString(), lsParameters.ToArray());
            return lsRet == null || lsRet.Count == 0 ? null : lsRet.ToList();
        }

		#endregion
	}
}
